package com.szosen.jkzj.activity;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.szosen.database.DBService;
import com.szosen.database.base_info;
import com.szosen.jkzj.R;
import com.szosen.util.TwoCloumAdapter;

public class HealthCheckList extends BaseActivity {
	private final String HEALTH_CHECK_TABLE = "HEALTHCHECK";
	private SQLiteDatabase db;
	private ListView lvCheckTableList;
	TextView txt_title;
	Button btn_new;
	private TwoCloumAdapter adapter;
	private static final int HEALTH_CHECK = 2;
	private List<String[]> datas;

	private boolean checkTextisNull(String text) {
		if (text == null) {
			return true;
		}
		if (TextUtils.isEmpty(text) || text.equals("null")) {
			return true;
		}
		return false;
	}

	public void finishActivity(View view) {
		onBackPressed();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.health_check_table_list);
		super.setTitleContent("【健康体检】");
		lvCheckTableList = (ListView) findViewById(R.id.lv_check_table_list);
		btn_new = (Button) findViewById(R.id.btn_htal_new);
		btn_new.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				addNewCheckTable();
			}

		});
		lvCheckTableList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// Map<String, String> m = (Map) parent
				// .getItemAtPosition(position);
				// 0为责任医生，1为体检时间
				String[] ss = (String[]) parent.getItemAtPosition(position);
				Intent intent = new Intent();
				intent.putExtra("time", ss[2]);// 用体检日期为唯一标准查询或者插入
				BaseActivity.setJktjTime(ss[2]);
				intent.putExtra("flag", "old");// 用体检日期为唯一标准查询或者插入
				intent.setClass(HealthCheckList.this, JKTJActivity2.class);
				startActivity(intent);
			}
		});

		lvCheckTableList
				.setOnItemLongClickListener(new OnItemLongClickListener() {

					@Override
					public boolean onItemLongClick(final AdapterView<?> parent,
							View view, final int position, long id) {

						AlertDialog.Builder builder = new AlertDialog.Builder(
								HealthCheckList.this);
						builder.setMessage("您确定要删除该体检表？");
						builder.setPositiveButton("确定", new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								String[] ss = (String[]) parent
										.getItemAtPosition(position);
								// 用体检日期为唯一标准查询或者插入
								String checkDate = ss[2];
								base_info base = new base_info();
								base.setTime(checkDate);
								DBService.delete(base);
								loadData();
							}
						});
						builder.setNegativeButton("取消", null);
						builder.create().show();
						return true;
					}
				});

	}

	@Override
	@SuppressLint("ResourceAsColor")
	protected void onResume() {
		super.onResume();
		loadData();
	}

	protected void updateDatabase(int pCoverReuslt, JSONObject pJson) {
	}

	private void loadData() {
		base_info base = new base_info();
		base.setIdCard(BaseActivity.getIdCard());
		List<base_info> lists = (List<base_info>) DBService.queryByIdCard(base);
		System.out.println("lists体检表偶的长度" + lists.size());
		datas = new ArrayList<String[]>();
		for (int j = 0; j < lists.size(); j++) {
			base_info bi = lists.get(j);
			// Map<String, String> map = new HashMap<String, String>();
			String[] data = new String[] { bi.getDoctorName(),
					bi.getCheckDate(), bi.getTime() };
			datas.add(data);
		}
		// adapter = new SimpleAdapter(HealthCheckList.this, datas,
		// R.layout.check_table_list_item, new String[] {
		// "doctorName", "checkDate" }, new int[] { R.id.tv_data1,
		// R.id.tv_data2, });
		adapter = new TwoCloumAdapter(context, datas);
		lvCheckTableList.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}

	protected void addNewCheckTable() {
		Intent intent = new Intent();
		intent.setClass(HealthCheckList.this, JKTJActivity2.class);
		intent.putExtra("flag", "new");
		startActivity(intent);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		adapter.notifyDataSetChanged();
	}

}
